package com.example.lending.View;


import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.fragment.app.Fragment;
import androidx.lifecycle.LiveData;
import androidx.lifecycle.Observer;
import androidx.lifecycle.SavedStateViewModelFactory;
import androidx.lifecycle.ViewModelProviders;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;

import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;

import com.example.lending.Model.User;
import com.example.lending.R;
import com.example.lending.ViewModel.UserViewModel;

import java.util.List;

/**
 * A simple {@link Fragment} subclass.
 */
public class FogetFragment extends Fragment {

    ImageView iv_left,iv_right;
    EditText et_email,et_pass1,et_pass2,et_username;
    Button button1,button2;
    UserViewModel userViewModel;
    User newUser;
    public FogetFragment() {
        // Required empty public constructor
    }
    void inteView(){
        et_email=getView().findViewById(R.id.et_foget_email);
        et_pass1=getView().findViewById(R.id.et_foget_password1);
        et_pass2=getView().findViewById(R.id.et_foget_password2);
        button1=getView().findViewById(R.id.btn_foget);
        button2=getView().findViewById(R.id.btn_foget_back);
        et_username=getView().findViewById(R.id.et_foget_username);
        userViewModel= ViewModelProviders.of(getActivity()).get(UserViewModel.class);
    }

    @Override
    public void onActivityCreated(@Nullable Bundle savedInstanceState) {
        super.onActivityCreated(savedInstanceState);
        inteView();

        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(final View v) {
                final String username=et_username.getText().toString();
                String email=et_email.getText().toString();
                final String password1=et_pass1.getText().toString();
                String password2=et_pass2.getText().toString();
                if (TextUtils.isEmpty(username)||TextUtils.isEmpty(email)||TextUtils.isEmpty(password1)||
                        TextUtils.isEmpty(password2)){
                    Toast.makeText(getActivity(), "请补充填完所有数据", Toast.LENGTH_SHORT).show();
                }else {
                    if (password1.equals(password2)) {
                        LiveData<List<User>>liveData = userViewModel.findUserWithPattern(username);
                        liveData.removeObservers(getViewLifecycleOwner());
                        final int[] x = {0};
                        liveData.observe(getViewLifecycleOwner(), new Observer<List<User>>() {
                            @Override
                            public void onChanged(List<User> users) {
                                if (users.isEmpty()) {
                                    Toast.makeText(getActivity(), "没有查询到这个账号", Toast.LENGTH_SHORT).show();
                                } else {
                                        for (int i = 0; i < users.size(); i++) {
                                            User user=getUser(users.get(i).getName(), password1, users.get(i).getAge(), users.get(i).getClassnum(), users.get(i).getEmail(),
                                                    users.get(i).getType());
                                            userViewModel.deleteUsers(users.get(i));
                                            userViewModel.insertUsers(newUser);
                                        }
                                        NavController controller = Navigation.findNavController(v);
                                        controller.navigate(R.id.action_fogetFragment_to_loginFragment2);
                                }
                                x[0] =55555;
                                System.out.println("bbbbbbbbbbbbbbbbbbbbb");
                            }
                        });

                    } else {
                        Toast.makeText(getActivity(), "两次密码不一样", Toast.LENGTH_SHORT).show();
                    }

                }
            }
        });
        button2.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                NavController controller=Navigation.findNavController(v);
                controller.navigate(R.id.action_fogetFragment_to_loginFragment2);
            }
        });
    }
    public User getUser(String name ,String password,int age,String classs,String email,int type){
        newUser=new User();
        newUser.setEmail(email);
        newUser.setPassword(password);
        newUser.setName(name);
        newUser.setType(type);
        newUser.setAge(age);
        newUser.setClassnum(classs);
        return newUser;
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_foget, container, false);
    }

}
